<?php
/**
 * ============================================================================
 * @package Forum
 * @author Andy
 * @version 16:15
 * @link https://sjdphp.com
 * ----------------------------------------------------------------------------
 */

namespace app\common\controller;


use app\BaseController;
use app\common\model\Nav;
use app\common\model\System;
use app\common\server\SystemLog;
use think\facade\View;

class HomeBaseController extends BaseController
{

    protected $staticHtmlFile = '';
    protected $staticHtmlDir = '';

    protected function initialize()
    {
        $this->staticHtmlDir = '../template/cache/web/' . $this->request->controller() . '/';

        ## 获取系统配置数据
        $config = System::where(array("system_iden" => 'web'))->field("system_id,input_name,system_content")->select()->toArray();

        View::assign([
            'config' => $config,
        ]);

    }


    public function beforeBuild($param = array())
    {
        if (is_array($param)) {
            $param = implode("_", $param);
        }
        $this->staticHtmlFile = $this->staticHtmlDir . $this->request->action() . ($param ? $param : '') . '.html';

        if (mkdirs($this->staticHtmlDir)) {
            if (file_exists($this->staticHtmlFile) && filectime($this->staticHtmlFile) >= time() - 60 * 60 * 24 * 5) {                  //静态文件存在
                header("Location:/" . $this->staticHtmlFile);exit();
            }
        }
    }

    public  function writeCacheHtml($html){
        if(!empty($this->staticHtmlFile) && !empty($html)) {
            if(file_exists($this->staticHtmlFile)) {
                unlink($this->staticHtmlFile);
            }
            if(file_put_contents($this->staticHtmlFile,$html)) {
                header("Location:/" . $this->staticHtmlFile);exit();
            }
        }
    }



}